const { ListToNode, NodeToList, ListNode, timeL } = require("../utils.js");

var longestValidParentheses = function (s) {
  let s1 = s,
    s2 = s.replace(/\(\)/g, "2|");

  const fun = (str) => {
    let list = str.split("|");
    let num = 0;
    for (let i = 0; i < list.length; i++) {
      num += +list[i];
    }
    return +num;
  };

  while (s1 !== s2) {
    let old = s2;
    s2 = s2.replace(/\(([0-9]+\|)+\)/g, (r) => {
      let num = fun(r.replace(/^\(|\)$/g, ""));
      return +num + 2 + "|";
    });
    s1 = old;
  }
  console.log(s1,s2)
  let res = 0;
  s2.replace(/([0-9]+\|)+/g, (r) => {
    let num = fun(r);
    res = res > num ? res : num;
    return r;
  });
  return res;
};

let s = ")(((((()())()()))()(()))(";

timeL(longestValidParentheses, [s]);
